﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DblogStudio.BLL;
using DblogStudio.DAL.Models;
using DblogStudio.GUI.Utils;
using DblogStudio.Utils;
namespace DblogStudio.GUI.HeThong
{
    public partial class FormNhatKyHoatDong : DblogStudio.GUI.Common.BaseDialog
    {
        bool allowUpdate = false;
        public FormNhatKyHoatDong()
        {
            InitializeComponent();
            dtFrom.Value = dtFrom.MinDate;
            dtTo.Value = DateTime.Now;
            InitGrid();
            LoadThaoTac();
            LoadLoaiDoiTuong();
            LoadNguoiThucHien();
        }

        void InitGrid()
        {
            gridData.AutoGenerateColumns = false;
            gridData.AddRowIndexing();
            colLoaiThaoTac.DataPropertyName = "LoaiThaoTacStr";
            colDoiTuong.DataPropertyName = "LoaiDoiTuongStr";
            colMaDoiTuong.DataPropertyName = "MaDoiTuong";
            colNguoiThucHien.DataPropertyName = "TenNguoiThucHien";
            colThoiGianGhiNhan.DataPropertyName = "ThoiGianGhiNhan";
            //colDuLieuCu.DataPropertyName = "DuLieuCuXML";
            //colDuLieuMoi.DataPropertyName = "DuLieuMoiXML";
        }

        void LoadThaoTac()
        {
            var ls = EnumWithName<LOAITHAOTAC>.ParseEnum();
            ls.Insert(0, new EnumWithName<LOAITHAOTAC> { Name = "Tất cả", Value = -1 });
            cmbThaoTac.DataSource = ls;
            cmbThaoTac.DisplayMember = "Name";
            cmbThaoTac.ValueMember = "Value";
        }

        void LoadLoaiDoiTuong()
        {
            var ls = EnumWithName<LOAIDOITUONG>.ParseEnum();
            ls.Insert(0, new EnumWithName<LOAIDOITUONG> { Name = "Tất cả", Value = -1 });
            cmbDoiTuong.DataSource = ls;
            cmbDoiTuong.DisplayMember = "Name";
            cmbDoiTuong.ValueMember = "Value";
        }

        void LoadNguoiThucHien()
        {
            var ls = BLL.HeThong.GetList();
            ls.Insert(0, new AppUser { UserName = "-1", FullName = "Tất cả" });
            cmbNguoiThucHien.DataSource = ls;

            cmbNguoiThucHien.DisplayMember = "FullName";
            cmbNguoiThucHien.ValueMember = "UserName";
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            allowUpdate = false;
            DateTime dtToGen = new DateTime(dtTo.Value.Year, dtTo.Value.Month, dtTo.Value.Day, 0, 0, 0);
            dtToGen = dtToGen.AddDays(1);
            var ls = BizNhatKyHoatDong.GetList(int.Parse(cmbThaoTac.SelectedValue.ToString()), int.Parse(cmbDoiTuong.SelectedValue.ToString()), cmbNguoiThucHien.SelectedValue.ToString(), dtFrom.Value, dtToGen, 1, (int)nSize.Value);
            bdNhatKy.DataSource = ls;
            gridData.DataSource = bdNhatKy;
            lbNhatKy.Text = "Danh sách này có: " + ls.Count + " hoạt động";
            allowUpdate = true;
            bdNhatKy_CurrentChanged(this, null);
        }

        private void bdNhatKy_CurrentChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                tvNewData.Nodes.Clear();
                tvOldData.Nodes.Clear();
                var current = bdNhatKy.Current as NhatKyHoatDong;
                if (current != null)
                {
                    var tenLoaiDoiTuong = EnumWithName<LOAIDOITUONG>.GetName((LOAIDOITUONG)current.LoaiDoiTuong);
                    if (!current.DuLieuCuXML.IsNullOrEmpty())
                    {
                        TreeNode rootNode = new TreeNode(tenLoaiDoiTuong);
                        tvOldData.Nodes.Add(rootNode);
                        XMLTreeView.PopulateTreeview(current.DuLieuCuXML, rootNode);
                        tvOldData.ExpandAll();
                    }
                    if (!current.DuLieuMoiXML.IsNullOrEmpty())
                    {
                        TreeNode rootNode = new TreeNode(tenLoaiDoiTuong);
                        tvNewData.Nodes.Add(rootNode);
                        XMLTreeView.PopulateTreeview(current.DuLieuMoiXML, rootNode);
                        tvNewData.ExpandAll();
                    }
                }
            }
        }
    }
}
